package com.toutou.base.bindhelper

import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableInt
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.tencent.mmkv.MMKV
import com.toutou.base.fragment.BaseFragment
import com.toutou.base.util.ActivitiesManager

class ViewPager2BDAdapter {
    companion object {
        private const val TAG = "ViewPager2BDAdapter"


        @JvmStatic
        @BindingAdapter(
            value = ["tabLayout", "originFragment", "fragments", "selectItem"],
            requireAll = false
        )
        fun init(
            viewPager2: ViewPager2,
            tabLayout: TabLayout,
            originFragment: Fragment?,
            fragments: Array<BaseFragment<*, *>>?,
            currentItem: Int
        ) {
            if (fragments == null) {
                return
            }
            if (originFragment == null) {
                viewPager2.adapter = object :
                    FragmentStateAdapter(ActivitiesManager.topActivity() as FragmentActivity) {
                    override fun getItemCount(): Int {
                        return fragments.size
                    }

                    override fun createFragment(position: Int): Fragment {
                        return fragments[position]
                    }

                    override fun getItemId(position: Int): Long {
                        return fragments[position].hashCode().toLong()
                    }
                }
            } else {
                viewPager2.adapter = object : FragmentStateAdapter(originFragment) {
                    override fun getItemCount(): Int {
                        return fragments.size
                    }

                    override fun createFragment(position: Int): Fragment {
                        return fragments[position]
                    }

                    override fun getItemId(position: Int): Long {
                        return fragments[position].hashCode().toLong()
                    }
                }
            }


            TabLayoutMediator(tabLayout, viewPager2) { tab, position ->
                tab.text = BaseFragment.getFragmentName(fragments[position].initFragmentNameType())
            }.attach()


            viewPager2.setCurrentItem(currentItem, false)
        }

        @JvmStatic
        @BindingAdapter("scrollable")
        fun setScrollable(viewPager2: ViewPager2, scrollable: Boolean) {
            viewPager2.isUserInputEnabled = scrollable
        }

        @JvmStatic
        @BindingAdapter(
            value = ["autoRefresh", "currentItem", "recordStateKey"],
            requireAll = false
        )
        fun setAutoRefresh(
            viewPager2: ViewPager2,
            autoRefresh: Boolean,
            currentItem: ObservableInt?,
            recordStateKey: String?
        ) {
            viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    currentItem?.set(position)
                    if (recordStateKey != null) {
//                            保存当前状态
                        MMKV.mmkvWithID("app").encode(recordStateKey, position)
                    }
                    if (autoRefresh) {
                        val fragment =
                            (viewPager2.adapter as FragmentStateAdapter).createFragment(position)
                        //刷新当前fragment
                        if (fragment is BaseFragment<*, *>) {
                            fragment.onLazyInitView()
                        }
                    }
                }
            })
        }
    }
}